package com.zhangh.summer.admin.support.prometheus.common.client;

import io.prometheus.client.Counter;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

/**
 * @description: TODO
 * @author: zhanghui  zhanghlucky@163.com
 * @create: 2020-08-26 14:48
 **/
@Slf4j
public class CounterClient {

    private Map<String, Counter> COUNTER_MAP=new HashMap<>();

    public CounterClient register(String name,String help,String... labelNames){
        if (null==COUNTER_MAP.get(name)) {
            Counter counter = Counter.build().
                    name(name).
                    labelNames(labelNames).
                    help(help).
                    register();
            COUNTER_MAP.put(name, counter);
            log.info("Metrics Counter register name:{},help:{},labelNames;{}", name, help, labelNames);
        }else{
            log.warn("Metrics Counter exist name:{}",name);
        }
        return this;
    }
    public CounterClient register(String name, Counter counter){
        if (null==COUNTER_MAP.get(name)) {
            counter.register();
            COUNTER_MAP.put(name,counter);
            log.info("Metrics Counter register name:{}",name);
        }else{
            log.warn("Metrics Counter exist name:{}",name);
        }
        return this;
    }
    public Counter get(String name){
        Counter counter=COUNTER_MAP.get(name);
        if (null!=counter){
            return counter;
        }
        log.error("can not find counter:{} ,please register it",name);
        return null;
    }
}
